//
//  DLGesPswBgView.m
//  delongzhitou
//
//  Created by Mr.Wang on 2018/12/28.
//  Copyright © 2018 delongzhitou. All rights reserved.
//

#import "DLGesPswBgView.h"
#import "DLGesPswManager.h"
#import "DLGesPswButton.h"

@interface DLGesPswBgView()
@property (nonatomic, strong) DLGesPswManager *manager;
@property (nonatomic, strong) NSMutableArray *selectorAry;//存储已经选择的按钮
@property (nonatomic, assign)CGPoint currentPoint;//当前处于哪个按钮范围内
@property (nonatomic, assign)GesPswButtonStateState status;//当前控件器所处状态(设置、重新设置、登录)
@property (nonatomic, assign)NSInteger inputNum;//输入的次数
@property (nonatomic, assign)NSInteger resetInputNum;//重置密码时验证旧密码 输入的次数
@property (nonatomic, strong)NSString *firstPassword;//表示设置密码时 第一次输入的手势密码
@end

@implementation DLGesPswBgView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.manager = [DLGesPswManager sharedInstance];
        self.selectorAry = [NSMutableArray array];
        [self setPropertiesByState:self.status];
        
        [self layoutSubControls];
        
    }
    return self;
}
- (void) layoutSubControls {
    NSInteger size = self.manager.circleRadius * 2;
    NSInteger margin = (kScreenWidth - 295)/2;
    float ins = 50;
    for (int i = 0; i < 9; i++) {
        NSInteger row = i/3;
        NSInteger col = i%3;
        DLGesPswButton *gesPswButton = [[DLGesPswButton alloc] initWithFrame:CGRectMake(ins+col*size+col*margin, row*size+row*margin, size, size)];
        gesPswButton.tag = i + 1;
        [self addSubview:gesPswButton];
    }
    [self setBackgroundColor:[UIColor clearColor]];
}
- (void)setPropertiesByState:(GesPswButtonStateState)buttonState{
    switch (buttonState) {
        case GesPswButtonStateStateNormal:
            [self setUserInteractionEnabled:YES];
            [self resetButtons];
            self.lineColor = self.manager.lineColorNormal;
            self.fillColor = self.manager.fillColorNormal;
            self.strokeColor = self.manager.strokeColorNormal;
            self.centerPointColor = self.manager.centerPointColorNormal;
            break;
        case GesPswButtonStateStateSelected:
            self.lineColor = self.manager.lineColorSelected;
            self.fillColor = self.manager.fillColorSelected;
            self.strokeColor = self.manager.strokeColorSelected;
            self.centerPointColor = self.manager.centerPointColorSelected;
            break;
        case GesPswButtonStateStateIncorrect:
            [self setUserInteractionEnabled:NO];
            self.lineColor = self.manager.lineColorIncorrect;
            self.fillColor = self.manager.fillColorIncorrect;
            self.strokeColor = self.manager.strokeColorIncorrect;
            self.centerPointColor = self.manager.centerPointColorIncorrect;
            [self performSelector:@selector(lockState:) withObject:[NSArray arrayWithObject:[NSNumber numberWithInteger:GesPswButtonStateStateNormal]] afterDelay:0.5f];
            break;
        default:
            break;
    }
}
- (void)lockState:(NSArray *)states {
    NSNumber *stateNumber = [states objectAtIndex:0];
    [self setPropertiesByState:[stateNumber integerValue]];
}
- (void)resetButtons {
    for (NSInteger i=0; i<[self.selectorAry count]; i++) {
        DLGesPswButton *button = self.selectorAry[i];
        [button setSelected:NO];
    }
    [self.selectorAry removeAllObjects];
    [self setNeedsDisplay];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
